package com.ruoyi.eam.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.eam.doman.EamStockD;
import com.ruoyi.eam.mapper.EamAssetMapper;
import com.ruoyi.eam.service.IEamAssetService;
import com.ruoyi.eam.service.IEamStockDService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.eam.doman.EamStock;
import com.ruoyi.eam.service.IEamStockService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 入库信息Controller
 *
 * @author xl
 * @date 2024-06-27
 */
@RestController
@RequestMapping("/stock")
public class EamStockController extends BaseController
{
    @Autowired
    private IEamStockService eamStockService;
    @Autowired
    private IEamStockDService eamStockDService;
    @Autowired
    private EamAssetMapper assetMapper;

    /**
     * 查询入库信息列表
     */
    @RequiresPermissions("eam:stock:list")
    @GetMapping("/list")
    public TableDataInfo list(EamStock eamStock)
    {
        startPage();
        List<EamStock> list = eamStockService.selectEamStockList(eamStock);
        return getDataTable(list);
    }

    /**
     * 导出入库信息列表
     */
    @RequiresPermissions("eam:stock:export")
    @Log(title = "入库信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EamStock eamStock)
    {
        List<EamStock> list = eamStockService.selectEamStockList(eamStock);
        ExcelUtil<EamStock> util = new ExcelUtil<EamStock>(EamStock.class);
        util.exportExcel(response, list, "入库信息数据");
    }

    /**
     * 获取入库信息详细信息
     */
    @RequiresPermissions("eam:stock:query")
    @GetMapping(value = "/{stockId}")
    public AjaxResult getInfo(@PathVariable("stockId") Long stockId)
    {
        return success(eamStockService.selectEamStockByStockId(stockId));
    }

    /**
     * 新增入库信息
     */
    @RequiresPermissions("eam:stock:add")
    @Log(title = "入库信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EamStock eamStock)
    {
        eamStock.setAssetId(assetMapper.queryAssetMaintain(null).get(assetMapper.queryAssetMaintain(null).size() -1).getAssetId());
        System.out.println("eamStock:"+eamStock);
        eamStockService.insertEamStock(eamStock);
        EamStockD eamStockD = new EamStockD();
        eamStockD.setAssetId(eamStock.getAssetId());
        if(eamStockDService.insertEamStockD(eamStockD) > 0){
            return success("入库成功");
        }
        return warn("该资产已入库");
    }

    /**
     * 修改入库信息
     */
    @RequiresPermissions("eam:stock:edit")
    @Log(title = "入库信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EamStock eamStock)
    {
        return toAjax(eamStockService.updateEamStock(eamStock));
    }

    /**
     * 删除入库信息
     */
    @RequiresPermissions("eam:stock:remove")
    @Log(title = "入库信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{stockIds}")
    public AjaxResult remove(@PathVariable Long[] stockIds)
    {
        return toAjax(eamStockService.deleteEamStockByStockIds(stockIds));
    }
}
